package com.yibo.feign;

import com.yibo.support.helper.ObjectHelper;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.yibo.feign.AuthTokenScheduledTask.YIBO_AUTH_TOKEN;
import static com.yibo.support.constant.YBConstant.AUTH;

@Component
public class RestTemplateInterceptor implements ClientHttpRequestInterceptor {


    @Override
    public ClientHttpResponse intercept(HttpRequest request,
                                        byte[] body,
                                        ClientHttpRequestExecution execution) throws IOException {

        //获取系统调用认证token
        String auth = System.getProperty(YIBO_AUTH_TOKEN);
        if(!ObjectHelper.isEmpty(auth)){
            request.getHeaders().set(AUTH,auth);
        }

        //获取当前线程请求token
        return execution.execute(request,body);
    }
}
